package cn.pugle.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.IOException;

import static cn.pugle.selenium.SeleUtil.waitElementDisappear;
import static cn.pugle.selenium.SeleUtil.waitElementPresent;

/**
 * 在td中创建监测活动的<p>
 *
 * @auther tzp on 2016/5/15.<br>
 */
public class TDMoniCamp {
    static RemoteWebDriver driver;
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        driver = SeleUtil.chromeDriver;

        try {
            driver.manage().deleteAllCookies();
            login();
            list();
//            for (int i = 135; i <= 136; i++) {
//                newTracking(i);
//            }
            int[] ids = new int[]{45, 41, 38};
            for (int id : ids) {
                newTracking(id);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            FileUtils.write(new File("/home/tzp/td2"), sb.toString(), true);
        }
    }

    public static boolean login() throws InterruptedException {
        driver.get("https://account.talkingdata.com");
        Thread.sleep(2000L);
        WebElement email = driver.findElementByName("email");
        email.click();
        email.sendKeys("chenzhiyuan@soyoung.com");

        WebElement password = driver.findElementById("password");
        new Actions(driver).moveToElement(password).click().perform();
        new Actions(driver).moveToElement(password).click().perform();
//        password.sendKeys("Sy2016");
        driver.executeScript("$(\"#password\").val(\"Sy2016\")");
        WebElement basic_login = driver.findElementById("basic_login");
        WebElement login = basic_login.findElement(By.className("btn-login"));
        login.click();
        Thread.sleep(2000L);
        return true;
    }

    public static boolean newTracking(int i) throws Exception {
        System.out.println("start");
        //新建活动
//        WebElement div = driver.findElementByClassName("search-export");
//        WebElement a = div.findElement(By.tagName("a"));
//        a.click();
        driver.get("https://www.talkingdata.com/tracking/manager.html?zh-CN&b0083b97564a#/new_spread?appkey=d9ff4f15f8c5486aa834fccd2d4b4d02&enter=summary");//
        Thread.sleep(1000L);

        waitElementPresent(By.id("channelId"));
//        WebElement channelId = driver.findElementById("channelId");
//        new Actions(driver).moveToElement(channelId).click().perform();
//        channelId.sendKeys("校园地推");
//        driver.executeScript("$(\"#channelId\").val(\"校园地推\")");
        driver.executeScript("(function(x){ x.$apply(function(){ x.channelName = '校园地推'; x.channelId = 21239; x.channelIdIsNull = false; }) })(angular.element($(\"#channelId\")[0]).scope())");

//        WebElement name = driver.findElementById("name");
//        name.sendKeys("暑期检测" + i);
//        driver.executeScript("$(\"#name\").val(\"暑期检测" + i + "\")");
        driver.executeScript("(function(x){ x.$apply(function(){ x.name = '暑期检测" + i + "'; x.tip.name.status=false; }) })(angular.element($(\"#name\")[0]).scope())");

        waitElementPresent(By.cssSelector("[ng-model=\"shorturlTxt\"]"));
        WebElement shorturlTxt = driver.findElementByCssSelector("[ng-model=\"shorturlTxt\"]");
        String url = shorturlTxt.getAttribute("value");
        System.out.println(i + "------------" + url);
        sb.append("暑期检测").append(i).append(",").append(url).append("\n");
        WebElement save = driver.findElementByCssSelector("[ng-click=\"save()\"]");
        save.click();
        waitElementDisappear(By.id("channelId"));
        System.out.println("end");
        Thread.sleep(1000L);
        return true;
    }

    public static void list() throws InterruptedException {
        driver.get("https://www.talkingdata.com/tracking/product.html?zh-CN&9edd0128696a#/productList?appkey=d9ff4f15f8c5486aa834fccd2d4b4d02");
        Thread.sleep(3000L);
        driver.get("https://www.talkingdata.com/tracking/summary.html?zh-CN&b0083b97564a#/channel_list?appkey=d9ff4f15f8c5486aa834fccd2d4b4d02");
        Thread.sleep(2000L);
    }
}
